//index.js
//获取应用实例
const app = getApp()
var myapp = require('../../app.js');
/**********************************************************************************/


var interval;
const types = ['default', 'primary', 'warn']
const statesMapping = {
  'true': 'LED_ON',
  'false': 'LED_OFF'
}
const pageObject = {
  data: {
    isDisabled: true,
    switchChecked: false
  },

  switchChange(e) {
    this.setData({
      switchChecked: e.detail.value
    });
    myapp.my_write(statesMapping[e.detail.value]);
  },

  bluetoothStateChange(state) {
    this.setData({
      isDisabled: state === 'SUCCESS' ? false : true
    });
  },

  writeStateChange(state) {
    this.setData({
      switchChecked: state === 'SUCCESS' ? this.data.switchChecked : !this.data.switchChecked
    });
  },

  onLoad: function (options) {
    let that = this;
    app.watch(that.bluetoothStateChange, 'bluetoothState');
    app.watch(that.writeStateChange, 'writeState');
  },
}


Page(pageObject)